#!/bin/bash

source ./envsetup

UDISK=$WS/u-disk

OUT_ZSBL=$WS_LINUX/out-zsbl
OUT_SBI=$WS_LINUX/out-sbi
OUT_LINUX=$WS_LINUX/out-linuxboot

if [[ -z "${DEV}" ]]; then
	DEV=sdb1
fi

sudo umount $UDISK
sudo mount /dev/$DEV $UDISK
if [ $? -ne 0 ]; then
	echo "ERROR: mount failed"
	exit 1
fi

if [ ! -f $UDISK/FLAG_LINUXBOOT ]; then
	echo "ERROR: not linuxboot sdcard, pleaes double-check!"
	exit 1
fi

if [ "$1" == "-a" ]; then
	echo "Removing zsbl and sbi ......"
	sudo rm $UDISK/zsbl.bin
	sudo rm $UDISK/riscv64/fw_dynamic.bin
	echo "Removing Done!"

        echo "Copying zsbl and sbi to sd-card"
        sudo cp $OUT_ZSBL/zsbl.bin $UDISK/zsbl.bin
        sudo cp $OUT_SBI/platform/generic/firmware/fw_dynamic.bin $UDISK/riscv64/fw_dynamic.bin
        echo "Copying Done!"
fi

echo "Removing dtb and linuxboot ......"
sudo rm $UDISK/riscv64/mango-milkv-pioneer.dtb
sudo rm $UDISK/riscv64/riscv64_Image
echo "Removing Done!"

echo "Copying dtb and linuxboot to sd-card"
sudo cp $OUT_LINUX/arch/riscv/boot/dts/sophgo/mango-milkv-pioneer.dtb $UDISK/riscv64/mango-milkv-pioneer.dtb
sudo cp $OUT_LINUX/arch/riscv/boot/Image $UDISK/riscv64/riscv64_Image
echo "Copying Done!"

sudo umount $UDISK

echo "Done!"

